#!coding=utf-8
from inspect import currentframe
from collections import deque



DEFAULT_MTU=1464
PACK_END_FLAG='\x00E\x00N\x00D\x00'
PACK_END_FLAG_LEN=len(PACK_END_FLAG)
GREECKET_NOOP=0.001

_SID=2010
mevid=lambda sid=_SID: currentframe().f_back.f_lineno+sid
_sid=mevid()
[
GREE_READY,
GREE_LISTEN,
GREE_CONNECTING,
GREE_CONNECTIONLOST,
GREE_CONNECTIONMADE,
GREE_DATARECEIVED,#完整數據組接收完成
GREE_PACKRECEIVED,#數據包接收完成
GREE_DATATOSEND,#數據準備發送
GREE_DATASEND,#完整數據組發送完成
GREE_PACKSEND,#數據包發送完成
GREE_ERROR,
GREE_ERROR_RECV,
GREE_ERROR_SEND,
GREE_ERROR_LISTEN,
GREE_ERROR_CONNECT,
GREE_SHUTDOWN,
GREE_ONLINE,
GREE_OFFLINE
]=range(_SID,_SID+mevid()-_sid-2)


GREE_CUSTOM=mevid()
EVENT_CALL=lambda p,c,e:c(e)
EVENT_LOOP_DEFAULT=0
EVENT_LOOP_WX=1
EVENT_LOOP_QT=2
EVENT_LOOP=EVENT_LOOP_DEFAULT


            
class GreeEvent(object):
    def __init__(self,eventid,data,peer,**kws):
        '''
        eventid,發生事件的id
        data,事件相應數據
        peer,發生事件的鏈接,(鏈接,遠端鏈接id)
        '''
        self._EventId=eventid
        self._Data=data
        self._Peer=peer
        self._EventObject=peer
        self._AsList=[eventid,data,peer]
        self.__dict__.update(kws)
        self._Skip=False

    def __getitem__(self,index):
        if index < len(self._AsList):
            return self._AsList[index]
        return None

    def __getattr__(self,name):
        return self._dict__[name]

    def skip(self):
        '''
        設置事件執行標誌,self._Skip為True時,事件將不被執行
        '''
        self._Skip=True

    @property
    def Skip(self):
        return self._Skip

    @property
    def AsList(self):
        return self._AsList

    @property
    def Peer(self):
        return self._Peer

    @property
    def EventId(self):
        return self._EventId

    @property
    def Data(self):
        return self._Data

    @property
    def EventObject(self):
        return self._EventObject

EVTBINDS={
            GREE_READY:deque([]),
            GREE_LISTEN:deque([]),
            GREE_CONNECTING:deque([]),    
            GREE_CONNECTIONLOST:deque([]),
            GREE_CONNECTIONMADE:deque([]),
            GREE_DATARECEIVED:deque([]),
            GREE_PACKRECEIVED:deque([]),
            GREE_DATASEND:deque([]),
            GREE_PACKSEND:deque([]),
            GREE_ERROR:deque([]),
            GREE_ERROR_RECV:deque([]),
            GREE_ERROR_SEND:deque([]),
            GREE_ERROR_LISTEN:deque([]),
            GREE_ERROR_CONNECT:deque([]),
            GREE_ONLINE:deque([]),
            GREE_OFFLINE:deque([])
}
